#include "widget.h"
#include "ui_widget.h"

#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 使用快捷键,设置'-'为减少,'+'为增加
    QShortcut *shortcut1 = new QShortcut(this);
    shortcut1->setKey(QKeySequence(Qt::Key_Minus));
    QShortcut *shortcut2 = new QShortcut(this);
    shortcut2->setKey(QKeySequence(Qt::Key_Equal)); // 设置=键,否则+需要shift+=才有反应
    // 绑定信号槽,快捷键按下触发回调函数
    connect(shortcut1, &QShortcut::activated, this, &Widget::subValue);
    connect(shortcut2, &QShortcut::activated, this, &Widget::addValue);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText("当前的值为: " + QString::number(value));
}

void Widget::subValue()
{
    int value = ui->horizontalSlider->value();
    if(value <= ui->horizontalSlider->minimum()) {
        return;
    }
    ui->horizontalSlider->setValue(value - 5);
}

void Widget::addValue()
{
    int value = ui->horizontalSlider->value();
    if(value >= ui->horizontalSlider->maximum()) {
        return;
    }
    ui->horizontalSlider->setValue(value + 5);
}

